package com.cnfilm.web.admin;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.cnfilm.common.GatherUtil;
import com.cnfilm.gather.GatherBean;
import com.cnfilm.gather.GatherURL;
import com.cnfilm.interceptor.AdminInterceptor;
import com.cnfilm.utils.CacheManager;
import com.cnfilm.utils.DateUtils;
import com.cnfilm.utils.OperateImage;
import com.cnfilm.utils.PingYinUtil;
import com.cnfilm.utils.ReadPropertity;
import com.cnfilm.utils.StringUtils;
import com.cnfilm.vo.AdminVO;
import com.cnfilm.vo.MsgVO;
import com.cnfilm.web.category.Category;
import com.cnfilm.web.code.Code;
import com.cnfilm.web.film.Film;
import com.cnfilm.web.gbook.Gbook;
import com.cnfilm.web.links.Links;
import com.cnfilm.web.tags.Tags;
import com.cnfilm.web.video.Video;
import com.cnfilm.web.website.Website;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

/**
 * 后台管理
 * @author jiang.li
 */
public class AdminController extends Controller {
	
	/**
	 * 采集数据
	 * @throws IOException 
	 */
	public void gather() throws IOException{
		List<GatherBean> gatherBeanList = GatherUtil.getGatherList();
		for(GatherBean gather:gatherBeanList){
			Record c = new Record();
			c.set("fname", gather.getName());
			c.set("nickname", gather.getName());
			String firstSpell = PingYinUtil.getFirstSpell(gather.getName());
			String split = "";
			for(int i=0;i<100;i++){
				firstSpell+=split;
				List<Film> filmList = Film.dao.find("select * from film where url = ?",firstSpell);
				if(filmList==null || filmList.size()==0){
					split = firstSpell;
					break;
				}
				split=(split+i);
			}
			c.set("url", split);
			c.set("addtime", DateUtils.getNowTime());
			c.set("clicknum", 0);
			c.set("pnum", 0);
			c.set("pabs", 0);
			c.set("psum", 0);
			c.set("director", "未知");
			c.set("actor", gather.getActor());
			c.set("starring", gather.getActor());
			c.set("content", gather.getJuqing());
			c.set("imgurl", gather.getImg());
			c.set("issuingdate", gather.getDate());
			c.set("language", "chinese");
			c.set("tags", gather.getTags());
			c.set("tid", "3");
			c.set("area", "china");
			c.set("cid", GatherURL.getIndex(gather.getType()));
			Db.save("film", c);
		}
		renderText("数据已经采集");
	}
	
	
	
	/**
	 * 创建HTML
	 */
	@SuppressWarnings("deprecation")
	public void createhtml(){
		String location = this.getRequest().getRealPath("/");
		boolean dd = createHTML("http://www.bdvcd.com/index",location+"index.html");
		if(dd){
			renderText("生成HTML成功");
		}else{
			renderText("生成HTML失败");
		}
	}
	
	/**
	 * 创建HTML
	 * @param url
	 * @param filename
	 * @throws IOException
	 */
	public static boolean createHTML(String url, String filename) {
		boolean dd = false;
		URL urlc = null;
		try {
			urlc = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) urlc.openConnection();
			InputStream ips = connection.getInputStream();
			FileOutputStream fos = new FileOutputStream(filename);
			challage(ips, fos);
			ips.close();
			fos.close();
			dd = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dd;
	}
	
	private static void challage(InputStream ips, OutputStream ops) throws IOException {
		byte[] contents = new byte[1024];
		int len = 0;
		while ((len = ips.read(contents)) != -1) {
		    ops.write(contents, 0, len);
		}
	}
	
	/**
	 * 清理缓存
	 */
	public void clearCache() {
		CacheManager.clearAll();
		renderText("清理缓存成功");
	}
	
	/**
	 * 后台登陆
	 */
	public void index() {
		render("/sysadmin/login.jsp");
	}
	
	/**
	 * 后台登陆方法
	 */
	public void login() {
		String verifyimg = getPara("verifyimg")==null?"":getPara("verifyimg");
		String rand = getSessionAttr("rand");
		if(verifyimg.equals(rand)){
			String username = getPara("username")==null?"":getPara("username");
			String password = getPara("password")==null?"":getPara("password");
			password = StringUtils.MD5(password);
			Admin admin = Admin.dao.findFirst("select * from admin where username = ? and password = ?",username,password);
			if(admin!=null){
				AdminVO adminvo = new AdminVO();
				adminvo.setId(admin.getInt("id"));
				adminvo.setUsername(username);
				adminvo.setPassword(password);
				setSessionAttr("sysadmin", adminvo);
				removeSessionAttr("rand");
				redirect("/sysadmin/index.jsp");
			}else{
				setAttr("msg", "账号输入错误,登录失败！");
				render("/sysadmin/login.jsp");
			}
		}else{
			setAttr("msg", "验证码输入错误,登录失败！");
			render("/sysadmin/login.jsp");
		}
	}
	
	/**
	 * 后台退出
	 */
	public void logout() {
		removeSessionAttr("sysadmin");
		redirect("/sysadmin/login.jsp");
	}
	
	/**
	 * 框架头部
	 */
	@Before(AdminInterceptor.class)
	public void top() {
		render("/sysadmin/common/top.jsp");
	}
	
	/**
	 * 框架左边
	 */
	@Before(AdminInterceptor.class)
	public void left() {
		render("/sysadmin/common/left.jsp");
	}
	
	/**
	 * 框架欢迎
	 */
	@Before(AdminInterceptor.class)
	public void main() {
		render("/sysadmin/common/main.jsp");
	}
	
	/**
	 * 获取管理账号
	 */
	@Before(AdminInterceptor.class)
	public void getadmin() {
		Admin admin = Admin.dao.findFirst("select * from admin where username=?","admin");
		setAttr("admin", admin);
		render("/sysadmin/admin.jsp");
	}
	
	/**
	 * 修改管理账号密码
	 */
	@Before(AdminInterceptor.class)
	public void updateadmin() {
		String id = getPara("id")==null?"":getPara("id");
		String password = getPara("password")==null?"":getPara("password");
		if(!password.equals("")){
			Admin admin = Admin.dao.findById(id);
			password = StringUtils.MD5(password);
			admin.set("password", password);
			boolean isok = admin.update();
			if(isok){
				setAttr("msg", "密码修改成功!");
			}else{
				setAttr("msg", "密码修改失败!");
			}
		}else{
			setAttr("msg", "密码未修改!");
		}
		forwardAction("/sysadmin/getadmin");
	}
	
	/**
	 * 获取分类
	 */
	@Before(AdminInterceptor.class)
	public void listcategory() {
		List<Category> categoryList = Category.dao.find("select * from category order by cid asc");
		setAttr("categoryList", categoryList);
		render("/sysadmin/categorylist.jsp");
	}
	
	/**
	 * 删除分类
	 */
	@Before(AdminInterceptor.class)
	public void delcategory() {
		int cid = getParaToInt("cid");
		boolean isok = Db.deleteById("category", "cid", cid);
		if(isok){
			setAttr("msg", "删除成功");
		}else{
			setAttr("msg", "删除失败!");
		}
		forwardAction("/sysadmin/listcategory");
	}
	
	/**
	 * 获取单个分类
	 */
	@Before(AdminInterceptor.class)
	public void getcategory() {
		int cid = getParaToInt("cid");
		Category category = Category.dao.findFirst("select * from category where cid = ?",cid);
		setAttr("category", category);
		render("/sysadmin/category.jsp");
	}
	
	/**
	 * 到分类添加页面
	 */
	@Before(AdminInterceptor.class)
	public void inputcategory() {
		render("/sysadmin/category.jsp");
	}
	
	/**
	 * 添加或者修改分类方法
	 */
	@Before(AdminInterceptor.class)
	public void updatecategory() {
		String scid = getPara("cid")==null?"":getPara("cid");
		Record c = new Record();
		c.set("cname", getPara("cname"));
		c.set("url", getPara("url"));
		c.set("title", getPara("title"));
		c.set("keywords", getPara("keywords"));
		c.set("description", getPara("description"));
		if(!scid.equals("")){
			c.set("cid", scid);
			Db.update("category", "cid", c);
			setAttr("msg", "修改成功");
		}else{
			Db.save("category", c);
			setAttr("msg", "添加成功");
		}
		forwardAction("/sysadmin/listcategory");
	}
	
	/**
	 * 检查分类重复
	 */
	@Before(AdminInterceptor.class)
	public void checkCategory() {
		MsgVO msg = new MsgVO();
		String cid = getPara("cid");
		String type = getPara("type");
		String value = getPara("value");
		if(cid.equals("0")){
			List<Category> categoryList = Category.dao.find("select * from category where "+type+"=?",value);
			if(categoryList!=null && categoryList.size()>0){
				msg.setState("false");
				msg.setMsg("已经存在");
			}else{
				msg.setState("true");
				msg.setMsg("可以使用");
			}
		}else{
			List<Category> categoryList = Category.dao.find("select * from category where "+type+"=? and cid!=?",value,cid);
			if(categoryList!=null && categoryList.size()>0){
				msg.setState("false");
				msg.setMsg("已经存在");
			}else{
				msg.setState("true");
				msg.setMsg("可以使用");
			}
		}
		
		renderJson(msg);
	}
	
	/**
	 * 到影片添加页面
	 */
	@Before(AdminInterceptor.class)
	public void inputfilm() {
		List<Category> categoryList = Category.dao.find("select * from category order by cid asc");
		setAttr("categoryList", categoryList);
		List<Code> languageList = Code.getCodeByType("language");
		setAttr("languageList", languageList);
		List<Code> issuingdateList = Code.getCodeByType("issuingdate");
		setAttr("issuingdateList", issuingdateList);
		List<Code> areaList = Code.getCodeByType("area");
		setAttr("areaList", areaList);
		setAttr("addtime", DateUtils.getNowTime());
		/**获取所有tags数据**/
		List<Tags> allTagsList = Tags.getAllTags();
		setAttr("tagList", allTagsList);
		render("/sysadmin/film.jsp");
	}
	
	/**
	 * 获取影片
	 */
	@Before(AdminInterceptor.class)
	public void getfilm() {
		List<Category> categoryList = Category.dao.find("select * from category order by cid asc");
		setAttr("categoryList", categoryList);
		
		List<Code> languageList = Code.getCodeByType("language");
		setAttr("languageList", languageList);
		
		List<Code> issuingdateList = Code.getCodeByType("issuingdate");
		setAttr("issuingdateList", issuingdateList);
		
		List<Code> areaList = Code.getCodeByType("area");
		setAttr("areaList", areaList);
		
		int fid = getParaToInt("fid");
		Film film = Film.dao.findFirst("select * from film where fid = ?",fid);
		setAttr("film", film);
		setAttr("addtime", film.get("addtime"));
		int cid = film.getInt("cid");
		Category category = Category.dao.findFirst("select * from category where cid = ?",cid);
		setAttr("category", category);
		/**获取所有tags数据**/
		List<Tags> allTagsList = Tags.getAllTags();
		setAttr("tagList", allTagsList);
		render("/sysadmin/film.jsp");
	}
	
	/**
	 * 分页影片
	 */
	@Before(AdminInterceptor.class)
	public void listfilm() {
		String word = getPara("word")==null?"": getPara("word");
		int pageNumber = getParaToInt("page")==null?1: getParaToInt("page");
		String where = null;
		if(word.equals("")){
			where = "from film order by fid desc";
		}else{
			where = "from film where fname like '%"+word+"%' order by fid desc";
		}
		Page<Film> filmList = Film.dao.paginate(pageNumber, 15, "select *", where);
		List<Film> newFilmList = new ArrayList<Film>();
		for(Film film:filmList.getList()){
			List<Code> vtypeList = Code.getCountByTypeAndFid("vtype", film.getInt("fid")+"");
			film.put("vtypeList",vtypeList);
			newFilmList.add(film);
		}
		setAttr("pageList", filmList);
		setAttr("filmList", newFilmList);
		setAttr("word", word);
		render("/sysadmin/filmlist.jsp");
	}
	

	/**
	 * 删除影片
	 */
	@Before(AdminInterceptor.class)
	public void delfilm() {
		int fid = getParaToInt("fid");
		boolean isok = Db.deleteById("film", "fid", fid);
		if(isok){
			setAttr("msg", "删除成功");
		}else{
			setAttr("msg", "删除失败!");
		}
		forwardAction("/sysadmin/listfilm");
	}
	
	/**
	 * 修改获取添加影片
	 */
	@Before(AdminInterceptor.class)
	public void updatefilm() {
		String fid = getPara("fid")==null?"":getPara("fid");
		Record c = new Record();
		c.set("fname", getPara("fname"));
		c.set("nickname", getPara("nickname"));
		c.set("cid", getPara("cid"));
		c.set("url", getPara("url"));
		c.set("director", getPara("director"));
		c.set("actor", getPara("actor"));
		c.set("starring", getPara("starring"));
		c.set("content", getPara("content"));
		c.set("imgurl", getPara("imgurl"));
		c.set("language", getPara("language"));
		c.set("issuingdate", getPara("issuingdate"));
		c.set("tags", getPara("tags"));
		c.set("tid", getPara("tid"));
		c.set("area", getPara("area"));
		c.set("addtime", getPara("addtime"));
		if(!fid.equals("")){
			c.set("fid", fid);
			Db.update("film", "fid", c);
			setAttr("msg", "修改成功");
		}else{
			c.set("clicknum", 0);
			c.set("pnum", 0);
			c.set("pabs", 0);
			c.set("psum", 0);
			Db.save("film", c);
			setAttr("msg", "添加成功");
		}
		forwardAction("/sysadmin/listfilm");
	}
	
	/**
	 * 播放源分页
	 */
	@Before(AdminInterceptor.class)
	public void listvideo() {
		String vtype = getPara("vtype");
		String fid = getPara("fid");
		int pageNumber = getParaToInt("page")==null?1: getParaToInt("page");
		StringBuffer where = new StringBuffer("from video where 1=1 ");
		where.append("and vtype=? and fid=?");
		where.append("order by vid desc");
		Page<Video> videoList = Video.dao.paginate(pageNumber, 15, "select *", where.toString(), vtype, fid);
		setAttr("pageList", videoList);
		setAttr("vtype", vtype);
		setAttr("vtypeName", Code.getNameByCodeAndType("vtype",vtype));
		setAttr("fid", fid);
		Film film = Film.dao.findFirst("select * from film where fid = ?",fid);
		setAttr("film", film);
		render("/sysadmin/videolist.jsp");
	}
	
	/**
	 * 到播放源添加页面
	 */
	@Before(AdminInterceptor.class)
	public void inputvideo() {
		String vtype = getPara("vtype");
		String fid = getPara("fid");
		Film film = Film.dao.findFirst("select * from film where fid = ?",fid);
		setAttr("film", film);
		List<Code> vtypeList = Code.getCodeByType("vtype");
		setAttr("vtypeList", vtypeList);
		setAttr("vtype", vtype);
		setAttr("vtypeName", Code.getNameByCodeAndType("vtype",vtype));
		setAttr("fid", fid);
		render("/sysadmin/video.jsp");
	}
	
	/**
	 * 修改获取添加播放源
	 */
	@Before(AdminInterceptor.class)
	public void updatevideo() {
		String vid = getPara("vid")==null?"":getPara("vid");
		String vtype = getPara("vtype")==null?"":getPara("vtype");
		Record c = new Record();
		c.set("vtitle", getPara("vtitle"));
		c.set("vshow", getPara("vshow"));
		c.set("vcontent", getPara("vcontent"));
		c.set("vurl", getPara("vurl"));
		c.set("furl", getPara("furl"));
		c.set("vtype", getPara("vtype"));
		c.set("fid", getPara("fid"));
		if(!vid.equals("")){
			c.set("vid", vid);
			Db.update("video", "vid", c);
			setAttr("msg", "修改成功");
		}else{
			c.set("clicknum", 0);
			c.set("addtime", DateUtils.getNowTime());
			Db.save("video", c);
			setAttr("msg", "添加成功");
		}
		redirect("/sysadmin/listvideo?fid="+getPara("fid")+"&vtype="+vtype);
	}
	
	/**
	 * 删除播放源
	 */
	@Before(AdminInterceptor.class)
	public void delvideo() {
		String vid = getPara("vid")==null?"":getPara("vid");
		String fid = getPara("fid")==null?"":getPara("fid");
		String vtype = getPara("vtype")==null?"":getPara("vtype");
		Record c = new Record();
		c.set("vid", vid);
		boolean ok = Db.delete("video", "vid", c);
		if(ok){
			setAttr("msg", "删除成功");
		}else{
			setAttr("msg", "删除失败");
		}
		redirect("/sysadmin/listvideo?fid="+fid+"&vtype="+vtype);
	}
	
	/*** 到图片上传页面 */
	public void image() {
		render("/sysadmin/common/image.jsp");
	}

	/*** 上传图片 */
	@SuppressWarnings("deprecation")
	public void upload() {
		String fileTime = DateUtils.getNowTime(DateUtils.DATE_KEY_STR);
		String filePath = this.getRequest().getRealPath(ReadPropertity.getProperty("actorpictmp"));
		UploadFile uploadFile = this.getFile("imgFile", filePath);
		String fileName = uploadFile.getFileName();
		File sFile = new File(this.getRequest().getRealPath(ReadPropertity.getProperty("actorpictmp") + fileName));
		String newFileName = fileTime + "."+ StringUtils.getExtensionName(fileName);
		File dFile = new File(this.getRequest().getRealPath(ReadPropertity.getProperty("actorpictmp") + newFileName));
		sFile.renameTo(dFile);
		setAttr("fileName", newFileName);
		render("/sysadmin/common/cut.jsp");
	}

	/*** 剪切图片 */
	@SuppressWarnings("deprecation")
	public void cut() {
		int x = getParaToInt("x");
		int y = getParaToInt("y");
		int width = getParaToInt("width");
		int height = getParaToInt("height");
		String fileName = getPara("fileName");
		String filePath = this.getRequest().getRealPath(ReadPropertity.getProperty("actorpictmp") + fileName);
		try {
			OperateImage o = new OperateImage(x, y, width, height);
			o.setSrcpath(filePath);
			String newFilePath = this.getRequest().getRealPath(ReadPropertity.getProperty("actorpic") + fileName);
			o.setSubpath(newFilePath);
			o.cut();
			File f = new File(filePath);
			f.delete();
			setAttr("fileName", fileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		render("/sysadmin/common/image.jsp");
	}
	
	/**
	 * 获取播放源
	 */
	@Before(AdminInterceptor.class)
	public void getvideo() {
		String vtype = getPara("vtype");
		String fid = getPara("fid");
		String vid = getPara("vid");
		Film film = Film.dao.findFirst("select * from film where fid = ?",fid);
		setAttr("film", film);
		List<Code> vtypeList = Code.getCodeByType("vtype");
		setAttr("vtypeList", vtypeList);
		setAttr("vtype", vtype);
		setAttr("vtypeName", Code.getNameByCodeAndType("vtype",vtype));
		setAttr("fid", fid);
		Video video = Video.dao.findFirst("select * from video where vid=?", vid);
		setAttr("video", video);
		render("/sysadmin/video.jsp");
	}
	
	/**
	 * 根据中文获取影片url
	 */
	@Before(AdminInterceptor.class)
	public void getfilmurl() {
		MsgVO msg = new MsgVO();
		String fname = getPara("fname");
		String firstSpell = PingYinUtil.getFirstSpell(fname);
		String split = "";
		for(int i=0;i<100;i++){
			firstSpell+=split;
			List<Film> filmList = Film.dao.find("select * from film where url = ?",firstSpell);
			if(filmList==null || filmList.size()==0){
				msg.setState("true");
				msg.setMsg(firstSpell);
				break;
			}
			split=(split+i);
		}
		renderJson(msg);
	}
	
	/**
	 * 网站配置分页
	 */
	@Before(AdminInterceptor.class)
	public void listwebsite() {
		int pageNumber = getParaToInt("page")==null?1: getParaToInt("page");
		StringBuffer where = new StringBuffer("from website where 1=1 ");
		where.append("order by id asc");
		Page<Website> pageList = Website.dao.paginate(pageNumber, 10, "select *", where.toString());
		setAttr("pageList", pageList);
		render("/sysadmin/websitelist.jsp");
	}
	
	/**
	 * 删除网站配置
	 */
	@Before(AdminInterceptor.class)
	public void delwebsite() {
		int id = getParaToInt("id");
		boolean isok = Db.deleteById("website", "id",id);
		if(isok){
			setAttr("msg", "删除成功");
		}else{
			setAttr("msg", "删除失败!");
		}
		forwardAction("/sysadmin/listwebsite");
	}
	
	/**
	 * 获取网站配置
	 */
	@Before(AdminInterceptor.class)
	public void getwebsite() {
		String id = getPara("id");
		Website website = Website.dao.findFirst("select * from website where id = ?",id);
		setAttr("website", website);
		render("/sysadmin/website.jsp");
	}
	
	/**
	 * 修改网站配置
	 */
	@Before(AdminInterceptor.class)
	public void updatewebsite() {
		String id = getPara("id")==null?"":getPara("id");
		Record c = new Record();
		c.set("code", getPara("code"));
		c.set("name", getPara("name"));
		c.set("content", getPara("content"));
		if(!id.equals("")){
			c.set("id", id);
			Db.update("website", "id", c);
			setAttr("msg", "修改成功");
		}else{
			Db.save("website", c);
			setAttr("msg", "添加成功");
		}
		redirect("/sysadmin/listwebsite");
	}
	
	/**
	 * 到网站配置添加页面
	 */
	@Before(AdminInterceptor.class)
	public void inputwebsite() {
		render("/sysadmin/website.jsp");
	}
	

	/**
	 * 网站留言分页
	 */
	@Before(AdminInterceptor.class)
	public void listgbook() {
		int pageNumber = getParaToInt("page")==null?1: getParaToInt("page");
		StringBuffer where = new StringBuffer("from gbook where 1=1 ");
		where.append("order by id desc");
		Page<Gbook> pageList = Gbook.dao.paginate(pageNumber, 10, "select *", where.toString());
		setAttr("pageList", pageList);
		render("/sysadmin/gbooklist.jsp");
	}
	
	/**
	 * 获取网站留言
	 */
	@Before(AdminInterceptor.class)
	public void getgbook() {
		String id = getPara("id");
		Gbook gbook = Gbook.dao.findFirst("select * from gbook where id = ?",id);
		setAttr("gbook", gbook);
		render("/sysadmin/gbook.jsp");
	}
	
	/**
	 * 修改网站配置
	 */
	@Before(AdminInterceptor.class)
	public void updategbook() {
		String id = getPara("id")==null?"":getPara("id");
		Record c = new Record();
		c.set("nickname", getPara("nickname"));
		c.set("replay", getPara("replay"));
		c.set("content", getPara("content"));
		c.set("addtime", getPara("addtime"));
		if(!id.equals("")){
			c.set("id", id);
			Db.update("gbook", "id", c);
			setAttr("msg", "修改成功");
		}else{
			Db.save("gbook", c);
			setAttr("msg", "添加成功");
		}
		redirect("/sysadmin/listgbook");
	}
	
	/**
	 * 到网站配置添加页面
	 */
	@Before(AdminInterceptor.class)
	public void inputgbook() {
		render("/sysadmin/gbook.jsp");
	}
	
	/**
	 * 删除留言
	 */
	@Before(AdminInterceptor.class)
	public void delgbook() {
		int fid = getParaToInt("id");
		boolean isok = Db.deleteById("gbook", "id", fid);
		if(isok){
			setAttr("msg", "删除成功");
		}else{
			setAttr("msg", "删除失败!");
		}
		forwardAction("/sysadmin/listgbook");
	}
	
	/**
	 * 友情链接分页
	 */
	@Before(AdminInterceptor.class)
	public void listlinks() {
		int pageNumber = getParaToInt("page")==null?1: getParaToInt("page");
		StringBuffer where = new StringBuffer("from links where 1=1 ");
		where.append("order by id desc");
		Page<Links> pageList = Links.dao.paginate(pageNumber, 10, "select *", where.toString());
		setAttr("pageList", pageList);
		render("/sysadmin/linkslist.jsp");
	}
	
	/**
	 * 获取友情链接
	 */
	@Before(AdminInterceptor.class)
	public void getlinks() {
		String id = getPara("id");
		Links links = Links.dao.findFirst("select * from links where id = ?",id);
		setAttr("links", links);
		render("/sysadmin/links.jsp");
	}
	
	/**
	 * 修改友情链接
	 */
	@Before(AdminInterceptor.class)
	public void updatelinks() {
		String id = getPara("id")==null?"":getPara("id");
		Record c = new Record();
		c.set("title", getPara("title"));
		c.set("url", getPara("url"));
		c.set("content", getPara("content"));
		if(!id.equals("")){
			c.set("id", id);
			Db.update("links", "id", c);
			setAttr("msg", "修改成功");
		}else{
			Db.save("links", c);
			setAttr("msg", "添加成功");
		}
		redirect("/sysadmin/listlinks");
	}
	
	/**
	 * 到友情链接添加页面
	 */
	@Before(AdminInterceptor.class)
	public void inputlinks() {
		render("/sysadmin/links.jsp");
	}
	
	/**
	 * 删除友情链接
	 */
	@Before(AdminInterceptor.class)
	public void dellinks() {
		int fid = getParaToInt("id");
		boolean isok = Db.deleteById("links", "id", fid);
		if(isok){
			setAttr("msg", "删除成功");
		}else{
			setAttr("msg", "删除失败!");
		}
		forwardAction("/sysadmin/listlinks");
	}
	
	/**
	 * 公共代码分页
	 */
	@Before(AdminInterceptor.class)
	public void listcode() {
		int pageNumber = getParaToInt("page")==null?1: getParaToInt("page");
		StringBuffer where = new StringBuffer("from code where 1=1 ");
		where.append("order by type,cid desc");
		Page<Code> pageList = Code.dao.paginate(pageNumber, 10, "select *", where.toString());
		setAttr("pageList", pageList);
		render("/sysadmin/codelist.jsp");
	}
	
	/**
	 * 获取公共代码
	 */
	@Before(AdminInterceptor.class)
	public void getcode() {
		String id = getPara("cid");
		Code code = Code.dao.findFirst("select * from code where cid = ?",id);
		setAttr("code", code);
		render("/sysadmin/code.jsp");
	}
	
	/**
	 * 修改公共代码
	 */
	@Before(AdminInterceptor.class)
	public void updatecode() {
		String cid = getPara("cid")==null?"":getPara("cid");
		Record c = new Record();
		c.set("type", getPara("type"));
		c.set("code", getPara("code"));
		c.set("name", getPara("name"));
		c.set("img", getPara("img"));
		c.set("content", getPara("content"));
		if(!cid.equals("")){
			c.set("cid", cid);
			Db.update("code", "cid", c);
			setAttr("msg", "修改成功");
		}else{
			Db.save("code", c);
			setAttr("msg", "添加成功");
		}
		redirect("/sysadmin/listcode");
	}
	
	/**
	 * 到公共代码添加页面
	 */
	@Before(AdminInterceptor.class)
	public void inputcode() {
		render("/sysadmin/code.jsp");
	}
	
	/**
	 * 删除公共代码
	 */
	@Before(AdminInterceptor.class)
	public void delcode() {
		int cid = getParaToInt("cid");
		boolean isok = Db.deleteById("code", "cid", cid);
		if(isok){
			setAttr("msg", "删除成功");
		}else{
			setAttr("msg", "删除失败!");
		}
		forwardAction("/sysadmin/listcode");
	}
	
	/**
	 * 标签
	 */
	@Before(AdminInterceptor.class)
	public void listtag() {
		List<Tags> tagList = Tags.dao.find("select * from tags order by tid desc");
		setAttr("taglist", tagList);
		render("/sysadmin/taglist.jsp");
	}
	
	/**
	 * 获取标签
	 */
	@Before(AdminInterceptor.class)
	public void gettag() {
		String tid = getPara("tid");
		Tags tags = Tags.dao.findFirst("select * from tags where tid = ?",tid);
		setAttr("tag", tags);
		render("/sysadmin/tag.jsp");
	}
	
	/**
	 * 修改标签
	 */
	@Before(AdminInterceptor.class)
	public void updatetag() {
		String tid = getPara("tid")==null?"":getPara("tid");
		Record c = new Record();
		c.set("tname", getPara("tname"));
		if(!tid.equals("")){
			c.set("tid", tid);
			Db.update("tags", "tid", c);
			setAttr("msg", "修改成功");
		}else{
			Db.save("tags", c);
			setAttr("msg", "添加成功");
		}
		redirect("/sysadmin/listtag");
	}
	
	/**
	 * 到标签添加页面
	 */
	@Before(AdminInterceptor.class)
	public void inputtag() {
		render("/sysadmin/tag.jsp");
	}
	
	/**
	 * 删除标签
	 */
	@Before(AdminInterceptor.class)
	public void deltag() {
		int tid = getParaToInt("tid");
		boolean isok = Db.deleteById("tags", "tid", tid);
		if(isok){
			setAttr("msg", "删除成功");
		}else{
			setAttr("msg", "删除失败!");
		}
		forwardAction("/sysadmin/listtag");
	}
	
}


